<script setup lang="ts">
import {useRouter, useRoute} from 'vue-router'

import {ServerUtils} from '@/serverUtils';

let route = useRoute()
let router = useRouter()
let pid = Number(route.params['pid'])
const findPostUrl = async (pid: number) => {
    let post = {} as any
    await ServerUtils.getPosts(null, pid, null, [], 1, 0).then(res => {
        if (!res?.total) {
            //404
            return
        } else {
            post = res.list[0]
        }
    })
    console.log(post)
    switch (post.type) {
        case '1': {
            if (post.tid) {
                return '/bbs/forums/topic/' + post.tid
            }
            else {
                return '/bbs/world/post/' + post.pid
            }
        }
        case '2': {
            if (post.subarea == '10001') {
                return `/bbs/world/post/${post.ppid}/${post.pid}`
            }
            else {
                return `/bbs/forums/post/${post.ppid}/${post.pid}`
            }
        }
        case '3':
            {
                await ServerUtils.getPosts(null, post.ppid, null, [], 1, 0).then(res => {
                    post = res?.list[0]
                })
                if (post.subarea == '10001') {
                    return `/bbs/world/post/${post.ppid}/${post.pid}`
                }
                else {
                    return `/bbs/forums/post/${post.ppid}/${post.pid}`
                }
            }
    }
    return ''
}
const refresh = async () => {
    const url = await findPostUrl(pid)
    console.log(url)
    router.replace({path: url})
}
refresh()

</script>
